package com.mgm.affsettings.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mgm.affsettings.service.AffsettingsService;
import com.mgm.constant.GobalConstant;
import com.mgm.dao.AdvertisementMapper;
import com.mgm.dao.AffsettingsMapper;
import com.mgm.dto.AffsettingsDto;
import com.mgm.dto.offer.OfferRoot;
import com.mgm.page.DataGrid;
import com.mgm.pojo.Advertisement;
import com.mgm.pojo.Affsettings;
import com.mgm.pojo.AffsettingsExample;
import com.mgm.pojo.AffsettingsExample.Criteria;
import com.mgm.result.IMessage;
import com.mgm.utils.NumberArithmeticUtils;

@Service
public class AffsettingsServiceImpl implements AffsettingsService {

	@Autowired
	private AffsettingsMapper affsettingsMapper;
	@Autowired
	private AdvertisementMapper advertisementMapper;
	
	@Override
	public DataGrid findAll(AffsettingsDto param, Integer pageNum, Integer pageSize) {
		//分页处理
		PageHelper.startPage(pageNum, pageSize);
		
		List<AffsettingsDto> list = affsettingsMapper.selectByParames(param);
		
		//创建一个返回值对象
		DataGrid dataGrid = new DataGrid();
		dataGrid.setItems(list);
		//获取PageInfo相关信息
		PageInfo<AffsettingsDto> pageInfo = new PageInfo<>(list);
		dataGrid.setTotal_count(pageInfo.getTotal()); //总记录数
		dataGrid.setPage_count(pageInfo.getPages()); // 总页数
		dataGrid.setPage_index(pageInfo.getPageNum()); // 当前页
		
		return dataGrid;
	}

	@Deprecated
	@Override
	public IMessage modifyPremium(Integer affId, Integer admentId, Integer advId, Integer premium) {
		Advertisement ad = advertisementMapper.selectByPrimaryKey(admentId);
		if(ad == null) return IMessage.error("该广告不存在！");
		if(ad.getAdv_id() != advId) return IMessage.error("该广告与广告主不匹配！");
		
		//查询
		AffsettingsExample example = new AffsettingsExample();
		//创建条件
		Criteria criteria = example.createCriteria();
		criteria.andAdffidEqualTo(affId);
		criteria.andAdvmentidEqualTo(admentId);
		
		List<Affsettings> list = affsettingsMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(list)) {
			return IMessage.error("修改溢价失败！");
		}
		Affsettings record = list.get(0);
		record.setPremium(premium);
		if(premium != null) { //如果溢价不为空，设置溢价后单价
			BigDecimal premiumPercent = NumberArithmeticUtils.safeDivide(premium, GobalConstant.PERCENT); //溢价/100
			record.setPremiumPrice(NumberArithmeticUtils.safeMultiply(premiumPercent, ad.getPay_price())); //溢价后单价
		} else {
			record.setPremiumPrice(null);
		}
		affsettingsMapper.updateByPrimaryKey(record);
		
		return IMessage.success();
	}

	@Override
	public IMessage modifyPremiumBatch(Integer affId, List<Integer> admentIds, Integer premium) {
		//查询
		AffsettingsExample example = new AffsettingsExample();
		//创建条件
		Criteria criteria = example.createCriteria();
		criteria.andAdffidEqualTo(affId);
		criteria.andAdvmentidIn(admentIds);
		
		List<Affsettings> list = affsettingsMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(list)) {
			return IMessage.error("修改溢价失败！");
		}
		for (Affsettings record : list) {
			Advertisement ad = advertisementMapper.selectByPrimaryKey(record.getAdvmentid());
			record.setPremium(premium);
			if(premium != null) { //如果溢价不为空，设置溢价后单价
				BigDecimal premiumPercent = NumberArithmeticUtils.safeDivide(premium, GobalConstant.PERCENT); //溢价/100
				record.setPremiumPrice(NumberArithmeticUtils.safeMultiply(premiumPercent, ad.getPay_price())); //溢价后单价
			} else {
				record.setPremiumPrice(null);
			}
			affsettingsMapper.updateByPrimaryKey(record);
		}
		return IMessage.success();
	}
	
	@Override
	public IMessage deleteByKeys(Integer affId, Integer admentId, Integer advId) {
		Advertisement ad = advertisementMapper.selectByPrimaryKey(admentId);
		if(ad == null) return IMessage.error("该广告不存在！");
		if(ad.getAdv_id() != advId) return IMessage.error("该广告与广告主不匹配！");
		//查询
		AffsettingsExample example = new AffsettingsExample();
		//创建条件
		Criteria criteria = example.createCriteria();
		criteria.andAdffidEqualTo(affId);
		criteria.andAdvmentidEqualTo(admentId);
		
		List<Affsettings> list = affsettingsMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(list)) {
			return IMessage.error("参数不合法，不能进行删除！");
		}
		Affsettings record = list.get(0);
		affsettingsMapper.deleteByPrimaryKey(record.getId());
		return IMessage.success();
	}

	@Override
	public IMessage updateEnabled(Integer affId, Integer admentId, Boolean enabled) {
		//查询
		AffsettingsExample example = new AffsettingsExample();
		//创建条件
		Criteria criteria = example.createCriteria();
		criteria.andAdffidEqualTo(affId);
		criteria.andAdvmentidEqualTo(admentId);
		
		List<Affsettings> list = affsettingsMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(list)) {
			return IMessage.error("无法开关操作！");
		}
		Affsettings record = list.get(0);
		record.setEnabled(enabled);
		affsettingsMapper.updateByPrimaryKeySelective(record);
		return IMessage.success();
	}
	
	@Override
	public IMessage updateEnabledBatch(Integer affId, List<Integer> admentIds, Boolean enabled) {
		//查询
		AffsettingsExample example = new AffsettingsExample();
		//创建条件
		Criteria criteria = example.createCriteria();
		criteria.andAdffidEqualTo(affId);
		criteria.andAdvmentidIn(admentIds);
		
		List<Affsettings> list = affsettingsMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(list)) {
			return IMessage.error("开关操作失败！");
		}
		for (Affsettings record : list) {
			record.setEnabled(enabled);
			affsettingsMapper.updateByPrimaryKeySelective(record);
		}
		return IMessage.success();
	}

	@Override
	public IMessage modifyCaps(Integer affId, Integer admentId, BigDecimal totalCap, BigDecimal dailyCap) {
		//查询
		AffsettingsExample example = new AffsettingsExample();
		//创建条件
		Criteria criteria = example.createCriteria();
		criteria.andAdffidEqualTo(affId);
		criteria.andAdvmentidEqualTo(admentId);
		
		List<Affsettings> list = affsettingsMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(list)) {
			return IMessage.error("无法操作！");
		}
		Affsettings record = list.get(0);
		record.setTotalCap(totalCap);
		record.setDailyCap(dailyCap);
		
		affsettingsMapper.updateByPrimaryKey(record);
		return IMessage.success();
	}

	@Override
	public Affsettings selectByAffIdAndAdmentId(Integer affId, Integer admentId) {
		return affsettingsMapper.selectByAffIdAndAdmentId(affId, admentId);
	}
	
	@Override
	public Affsettings selectByAffIdAndAdmentId(String affid, String admentid) {
		return affsettingsMapper.selectByAffIdStrAndAdmentIdStr(affid, admentid);
	}

	@Override
	public void insertCallAffsetting() {
		// TODO Auto-generated method stub
		affsettingsMapper.insertCallAffsetting();
	}

	@Override
	public List<OfferRoot> getAffOffer(Map<String, String> map) {
		List<OfferRoot> list = affsettingsMapper.getAffOffer(map);
		if(CollectionUtils.isEmpty(list)) {
			return new ArrayList<>();
		}
		return list;
	}

}
